<template>
	<body-wrap isFlex>
		<template #header>
			<view class="pbTopBox">
				<view class="icon" @click="date_add(-1)"><i class="iconfont icon-xiangzuojiantou"></i></view>
				<view class="time" @click="openDate">{{curDate}} {{getWeekday(curDate)}}</view>
				<view class="icon" @click="date_add(1)"><i class="iconfont icon-xiangyoujiantou"></i></view>
			</view>
		</template>
		
		<consult-time-table :type='1' :lists="lists || []" @preBook="goOrder"/>
		<empty v-if="lists && lists.length==0"/>

		<uni-datetime-picker ref="dtpRef" class="mydpdate" type="date" :clear-icon="false" v-model="curDate" @change="setDate">
			<template #default>
				<view></view>
			</template>
		</uni-datetime-picker>
	</body-wrap>
</template>

<script>
	import {defineComponent,reactive,toRefs,onMounted,getCurrentInstance,computed} from 'vue'
	import {onLoad,onShow} from '@dcloudio/uni-app'
	import { useStore } from 'vuex'
	import bodyWrap from "@/components/bodyWrap/index.vue"
	import cmImage from "@/components/cmImage/index.vue"
	import consultTimeTable from "@/components/consultTimeTable/index.vue"
	import mySearch from "@/components/mySearch/index.vue"
	import {getConsultList} from "@/api/consult.js"
	import {toast,dateFormat,getWeekday,dateAdd} from "@/utils/common.js"
	import empty from "@/components/empty/index.vue"
	export default defineComponent({
		components:{
			bodyWrap,
			cmImage,
			consultTimeTable,
			mySearch,
			empty
		},
		props:{
		
		},
		setup(props,context) {
			const store = useStore()
			const {proxy} = getCurrentInstance()
			
			const state = reactive({
				imgUrlPrefix:getApp().globalData.imgUrlPrefix,
				userInfo:computed(()=>store.state.base.userInfo),
				curDate:dateFormat('Y-m-d',new Date()),
				pageIndex:1,
				pageSize:20,
				kwd:'',
				teacherId:'',
				lists:''
			})
			
			onLoad(options=>{
				if(options && options.teacherId){
					state.teacherId = options.teacherId
				}
				get_consult_list()
			})
			
			onMounted(()=>{
				store.commit('base/init_orderInfo',{})
			})
			
			const get_consult_list = async ()=>{
				const res = await getConsultList({
					pageIndex:state.pageIndex,
					pageSize:state.pageSize,
					teacherId:state.teacherId || null,
					searchDate:state.curDate + ' 00:00:00'
				})
				if(res.code == 200){
					state.lists = res.data.data
				}
			}
			
			const openDate = ()=>{
				proxy.$refs.dtpRef.show()
			}
			
			const setDate = ()=>{
				//console.log("curDate:",state.curDate)
				get_consult_list()
			}
			
			const doSearch = ()=>{
				
			}
			const date_add = (num)=>{
				const cDate = dateAdd(num,state.curDate)
				state.curDate = dateFormat('Y-m-d',cDate)
				get_consult_list()
			}
			
			const goOrder =(item)=>{
				// const pages = getCurrentPages();
				// const currentPage = pages[pages.length - 1];
				// const fullPath = currentPage.$page.fullPath
				// store.commit('base/set_backUrl',currentPage.$page.fullPath)
				store.commit('base/set_backUrl','/pages/home/consult/consult')
				store.commit('base/set_orderInfo',{
					productId:item._id, // 商品id
					orderType:item.type==1?62:(item.type==2?61:''), // 61 线下预约订单，62 线上一对一语音咨询订单
					product:item, // 购买的产品信息
					price:item.price,// 商品价格
					name:item.title,// 商品名称
					sellerId:item.teacherId._id,  // 卖家id
					startTime:`${state.curDate} ${item.startTime}`,  // 预约开始时间
					endTime:`${state.curDate} ${item.endTime}`,  // 预约结束时间
					zxTime:`${state.curDate} ${item.startTime} ~ ${item.endTime}`  // 非订单需要字段，临时添加，用于后面页显示
				})
				uni.navigateTo({
					url:'/pages/preBook/add'
				})
			}
			
			return {
				...toRefs(state),
				setDate,
				openDate,
				doSearch,
				getWeekday,
				date_add,
				goOrder
			}
		}
	})
</script>

<style lang="scss" scoped>
	.pbTopBox{
		display: flex;
		height: 80rpx;
		align-items: center;
		padding: 0;
		background-color: #fff;
		border-bottom:1px solid #eee ;
		.icon{
			display: flex;
			align-items: center;
			justify-content: center;
			width: 100rpx;
			height: 100%;
			flex-shrink: 0;
		}
		.time{
			display: flex;
			height: 100%;
			justify-content: center;
			align-items: center;
			flex: 1;
			flex-shrink: 0;
		}
	}
</style>
